<?php
/****************************************************************************************************** 
	This class contains all queries that deal with the gban_server table
******************************************************************************************************/

require_once(ROOTDIR."/include/database/class.Database.php");
require_once(ROOTDIR."/config/class.Config.php");
require_once(ROOTDIR."/include/objects/class.Server.php");

class ServerQueries extends Config {	

	// Variables
	var $db; // Object of database class
	
	// Default constructor
	function __construct() {
		$this->init();
  }
  
	function ServerQueries() {
		$this->init();
  }
  
  function init() {
    $this->db = new Database; // Create new database connection for this object
  }
    
  /************************************************************************
	Add a new server
	************************************************************************/
  function addServer($name, $ip, $port, $rcon, $type) {
    $addServerQuery = "INSERT INTO gban_servers (name, ip, port, rcon, type)
    values('".addslashes($name)."', '".$ip."', '".$port."', '".addslashes($rcon)."', '".$type."')";
    $this->db->sql_query($addServerQuery);
    
    return $this->db->get_insert_id();
  }
  
  function deleteServer($id) {
    $deleteServerQuery = "DELETE FROM gban_servers WHERE server_id = '".$id."'";
    $this->db->sql_query($deleteServerQuery);
  }
  
  /************************************************************************
	Return an array of server objects
	************************************************************************/
  function getServers() {
    $serversQuery = "SELECT server_id, name, ip, port, rcon, type FROM gban_servers ORDER BY server_id";
    $this->db->sql_query($serversQuery);
    $servers = $this->db->get_array();
    
    $serverList = array();
    
    for($i=0; $i<count($servers); $i++) {
      $server = new Server();
      
      $server->setId($servers[$i]['server_id']);
      $server->setName(stripslashes($servers[$i]['name']));
      $server->setIp($servers[$i]['ip']);
      $server->setPort($servers[$i]['port']);
      $server->setRcon(stripslashes($servers[$i]['rcon']));
      $server->setType($servers[$i]['type']);
      
      array_push($serverList, $server); // Add the server object to the array
    }
    
    return $serverList;
  }
  
  /************************************************************************
	Get the number of servers
	************************************************************************/
  function getNumberOfServers() {
    $countQuery = "SELECT count(*) as count FROM gban_servers";
    $this->db->sql_query($countQuery);
    $count = $this->db->get_row();
    $count = $count['count'];
    return $count;
  }
  
  /************************************************************************
	Get a server based on the server's id
	************************************************************************/
  function getServer($id) {
    $serversQuery = "SELECT server_id, name, ip, port, rcon, type
                     FROM gban_servers 
                     WHERE server_id = '".$id."'";
    $this->db->sql_query($serversQuery);
    $tempServer = $this->db->get_row();
    
    $server = new Server();
    
    $server->setId($tempServer['server_id']);
    $server->setName(stripslashes($tempServer['name']));
    $server->setIp($tempServer['ip']);
    $server->setPort($tempServer['port']);
    $server->setRcon(stripslashes($tempServer['rcon']));
    $server->setType($tempServer[$i]['type']);
        
    return $server;
  }
  
  /************************************************************************
	Update the specified server's information
	************************************************************************/
  function updateServer($name, $ip, $port, $rcon, $type, $id) {
    $sql = "UPDATE gban_servers 
        SET name='".addslashes($name)."',
        ip = '".$ip."', 
        port = '".$port."',
        rcon = '".addslashes($rcon)."',
        type = '".$type."'
        WHERE server_id = '".$id."'";
    
    // Update the db
    $this->db->sql_query($sql);
  }
  
  /************************************************************************
	Get the first server that is listed in the server table
	************************************************************************/
  function getFirstServer() {
    $sql = "SELECT MIN( server_id ) as min FROM `gban_servers`";
    $this->db->sql_query($sql);
    $id = $this->db->get_row();
    return $id['min'];
  }
}
?>
